package com.nova.bluetooth.ui.bluetoothDemo;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothGatt;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.SeekBar;

import androidx.activity.EdgeToEdge;
import androidx.lifecycle.ViewModelProvider;

import com.dido.fastblelib.data.BleDevice;
import com.dido.fastblelib.exception.BleException;
import com.dido.fastblelib.utils.HexUtil;
import com.nova.bluetooth.MyApp;
import com.nova.bluetooth.R;
import com.nova.bluetooth.bean.DeviceConfigBean;
import com.nova.bluetooth.databinding.ActivityBluetoothControlBinding;
import com.nova.bluetooth.widget.AutoScrollTextView;
import com.nova.bluetooth.helpUtils.BluetoothDataUtil;
import com.nova.bluetooth.manage.BluetoothHelper;
import com.nova.bluetooth.widget.CommonDialog;
import com.nova.bluetooth.helpUtils.LogUtil;
import com.nova.bluetooth.helpUtils.MyDataCallback;
import com.nova.bluetooth.helpUtils.MyGattCallback;
import com.nova.bluetooth.helpUtils.ToastUtils;
import com.nova.bluetooth.myEnum.DenoiseMode;
import com.nova.bluetooth.myEnum.DenoiseModeLevel;
import com.nova.bluetooth.myEnum.EQModel;
import com.nova.bluetooth.myEnum.FindDeviceModel;
import com.nova.bluetooth.myEnum.ImmersionModel;
import com.nova.bluetooth.myEnum.KeyFunctionClickModel;
import com.nova.bluetooth.myEnum.KeyFunctionControlModel;
import com.nova.bluetooth.myEnum.LightModel;
import com.nova.bluetooth.myEnum.SleepModel;
import com.nova.bluetooth.myEnum.SpaceModel;
import com.nova.bluetooth.myEnum.TFModel;
import com.nova.bluetooth.permission.PermissionManageActivity;
import com.nova.bluetooth.permission.PermissionType;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@SuppressLint("SetTextI18n")
public class DemoBluetoothControlActivity extends PermissionManageActivity implements MyDataCallback, MyGattCallback {
    private DeviceConfigBean deviceConfigBean;
    private AutoScrollTextView autoScrollTextView;

    private ActivityBluetoothControlBinding binding;
    private NovaBluetoothControlViewModel mViewModel;

    private EQModel mEQModel = EQModel.SSQVP;

    private final View.OnClickListener onClickListener = v -> {
        if (!BluetoothHelper.getInstance().isConnected()) {
            if (v.getId() == R.id.iv_full_ui || v.getId() == R.id.iv_back || v.getId() == R.id.btn_save) {
                //do nothing
            } else {
                ToastUtils.show("蓝牙未连接,蓝牙操作无效");
                return;
            }
        }

        if (v.getId() == R.id.iv_back) {
            finish();
        } else if (v.getId() == R.id.iv_full_ui) {
            setFull();
        } else if (v.getId() == R.id.btn_save) {
            doBtnSave();
        } else if (v.getId() == R.id.iv_clean) {
            autoScrollTextView.setText("");
        } else if (v.getId() == R.id.iv_send_text) {
            doSendText();
        } else if (v.getId() == R.id.eq_radio_button_1 ||
                v.getId() == R.id.eq_radio_button_2 ||
                v.getId() == R.id.eq_radio_button_3 ||
                v.getId() == R.id.eq_radio_button_4 ||
                v.getId() == R.id.eq_radio_button_5 ||
                v.getId() == R.id.eq_radio_button_6) {
            sendEQCMD(v);
        } else if (v.getId() == R.id.btn_get_eq) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_EQ, true);
        } else if (v.getId() == R.id.btn_disconnect_ble) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_DISCONNECT, true);
        } else if (v.getId() == R.id.switch_ear_check) {
            BluetoothHelper.getInstance().sendByteArrayDataSafety(
                    BluetoothDataUtil.getEarDetectionCMD(binding.switchEarCheck.isChecked()));
        } else if (v.getId() == R.id.switch_game) {
            BluetoothHelper.getInstance().sendByteArrayDataSafety(
                    BluetoothDataUtil.getGameModelSetCMD(binding.switchGame.isChecked()));
        } else if (v.getId() == R.id.btn_reset_device) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_RESET, true);
        } else if (v.getId() == R.id.btn_double_battery) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_LEFT_BATTERY, true);
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_RIGHT_BATTERY, true);
        } else if (v.getId() == R.id.btn_bin_battery) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_BIN_BATTERY, true);
        } else if (v.getId() == R.id.left_double_radio_option1 ||
                v.getId() == R.id.left_double_radio_option2 ||
                v.getId() == R.id.left_double_radio_option3 ||
                v.getId() == R.id.left_double_radio_option4 ||
                v.getId() == R.id.left_double_radio_option5 ||
                v.getId() == R.id.right_double_radio_option1 ||
                v.getId() == R.id.right_double_radio_option2 ||
                v.getId() == R.id.right_double_radio_option3 ||
                v.getId() == R.id.right_double_radio_option4 ||
                v.getId() == R.id.right_double_radio_option5 ||
                v.getId() == R.id.left_triple_radio_option1 ||
                v.getId() == R.id.left_triple_radio_option2 ||
                v.getId() == R.id.left_triple_radio_option3 ||
                v.getId() == R.id.left_triple_radio_option4 ||
                v.getId() == R.id.left_triple_radio_option5 ||
                v.getId() == R.id.right_triple_radio_option1 ||
                v.getId() == R.id.right_triple_radio_option2 ||
                v.getId() == R.id.right_triple_radio_option3 ||
                v.getId() == R.id.right_triple_radio_option4 ||
                v.getId() == R.id.right_triple_radio_option5) {
            sendKeyFunctionCMD(v);
        } else if (v.getId() == R.id.btn_key_function_current_state) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION1, true);
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION2, true);
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION3, true);
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION4, true);
        } else if (v.getId() == R.id.btn_get_denoise) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_DENOISE, true);
        } else if (v.getId() == R.id.denoise_radio_button_1 ||
                v.getId() == R.id.denoise_radio_button_2 ||
                v.getId() == R.id.denoise_radio_button_3 ||
                v.getId() == R.id.level_radio_button_1 ||
                v.getId() == R.id.level_radio_button_2 ||
                v.getId() == R.id.level_radio_button_3) {
            sendDenoiseCMD(v);
        } else if (v.getId() == R.id.find_checkBox_1 ||
                v.getId() == R.id.find_checkBox_2 ||
                v.getId() == R.id.btn_cancel_find) {
            sendFindDeviceCMD(v);
        } else if (v.getId() == R.id.btn_get_sleep) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_SLEEP_MODEL, true);
        } else if (v.getId() == R.id.btn_cancel_time) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_SLEEP_TIME_CANCEL, true);
        } else if (v.getId() == R.id.btn_send_sleep_time) {
            setSleepTime();
        } else if (v.getId() == R.id.sleep_radio_button_1 ||
                v.getId() == R.id.sleep_radio_button_2 ||
                v.getId() == R.id.sleep_radio_button_3) {
            sendSleepCMD(v);
        } else if (v.getId() == R.id.btn_get_space_sound) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_SPACE_SOUND, true);
        } else if (v.getId() == R.id.space_sound_radio_button_1 ||
                v.getId() == R.id.space_sound_radio_button_2 ||
                v.getId() == R.id.space_sound_radio_button_3) {
            sendSpaceSoundCMD(v);
        } else if (v.getId() == R.id.btn_get_immersion) {
            BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_IMMERSION_SOUND, true);
        } else if (v.getId() == R.id.immersion_radio_button_1 ||
                v.getId() == R.id.immersion_radio_button_2 ||
                v.getId() == R.id.immersion_radio_button_3) {
            sendImmersionSoundCMD(v);
        } else if (v.getId() == R.id.switch_focus_model) {
            BluetoothHelper.getInstance().sendByteArrayDataSafety(
                    BluetoothDataUtil.getFocusModelSetCMD(binding.switchFocusModel.isChecked()));
        } else if (v.getId() == R.id.switch_ai_translate) {
            BluetoothHelper.getInstance().sendByteArrayDataSafety(
                    BluetoothDataUtil.getAiTranslateSetCMD(binding.switchAiTranslate.isChecked()));
        } else if (v.getId() == R.id.switch_ai_audio_transcription) {
            BluetoothHelper.getInstance().sendByteArrayDataSafety(
                    BluetoothDataUtil.getAiAudioTranscriptionSetCMD(binding.switchAiAudioTranscription.isChecked()));
        } else if (v.getId() == R.id.btn_get_volume) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_VOLUME, true);
        } else if (v.getId() == R.id.btn_volume_add) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_ADD_VOLUME, true);
        } else if (v.getId() == R.id.btn_volume_sub) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_SUB_VOLUME, true);
        } else if (v.getId() == R.id.btn_get_play_state) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_PLAY_STATE, true);
        } else if (v.getId() == R.id.btn_last) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_LAST, true);
        } else if (v.getId() == R.id.btn_play_pause) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_PLAY_PAUSE, true);
        } else if (v.getId() == R.id.btn_next) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_NEXT, true);
        } else if (v.getId() == R.id.btn_play) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_PLAY, true);
        } else if (v.getId() == R.id.btn_pause) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_PAUSE, true);
        } else if (v.getId() == R.id.btn_single_battery) {
            sendGetBatteryCMD();
        } else if (v.getId() == R.id.btn_get_tf_model) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_TF_MODEL, true);
        } else if (v.getId() == R.id.tf_bluetooth_radio_button_1 ||
                v.getId() == R.id.tf_bluetooth_radio_button_2) {
            sendTFModelCMD(v);
        } else if (v.getId() == R.id.btn_get_light) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_LIGHT, true);
        } else if (v.getId() == R.id.light_radio_button_1 ||
                v.getId() == R.id.light_radio_button_2 ||
                v.getId() == R.id.light_radio_button_3 ||
                v.getId() == R.id.light_radio_button_4 ||
                v.getId() == R.id.light_radio_button_5 ||
                v.getId() == R.id.light_radio_button_6) {
            sendLightSetCMD(v);
        } else if (v.getId() == R.id.btn_get_fm) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_FM, true);
        } else if (v.getId() == R.id.btn_get_radio) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_RADIO, true);
        } else if (v.getId() == R.id.tv_last_radio) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_LAST_FM, true);
        } else if (v.getId() == R.id.tv_mute_set) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_MUTE_SET, true);
        } else if (v.getId() == R.id.tv_next_radio) {
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_NEXT_FM, true);
        } else if (v.getId() == R.id.switch_fm) {
            BluetoothHelper.getInstance().sendByteArrayDataSafety(
                    BluetoothDataUtil.getFMModelSetCMD(
                            binding.includeFmSet.switchFm.isChecked()));
        } else if (v.getId() == R.id.btn_one_test_get_state) {
//            oneTestGetState();
        } else if (v.getId() == R.id.btn_one_test_fuction) {
//            oneTestFunction();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityBluetoothControlBinding.inflate(getLayoutInflater());
        mViewModel = new ViewModelProvider(MyApp.getInstance()).get(NovaBluetoothControlViewModel.class);
        binding.setViewModel(mViewModel);
        binding.setLifecycleOwner(this);
        setContentView(binding.getRoot());

        initView();
        initData();
        updateUIByConfigData();
        observe();

        setPaddingBottom(this, binding.getRoot());
//        test();
    }

    private void observe() {
        mViewModel.eqModelMutableLiveData.observe(this, eqModelMutableLiveData -> {
            mEQModel = eqModelMutableLiveData;
            changeEQView();
        });
        mViewModel.eqSetBoxResultMutableLiveData.observe(this, eqSetBoxResultMutableLiveData -> {
            if (eqSetBoxResultMutableLiveData == 0) {
                changeEQView();
            }
        });
        mViewModel.keyFunctionControlModel_L_D_LiveData.observe(this, keyFunctionControlModel_L_D_LiveData -> {
            keyFunctionControlModel_L_D = keyFunctionControlModel_L_D_LiveData;
            changeKeyFunctionView();
        });
        mViewModel.keyFunctionControlModel_R_D_LiveData.observe(this, keyFunctionControlModel_R_D_LiveData -> {
            keyFunctionControlModel_R_D = keyFunctionControlModel_R_D_LiveData;
            changeKeyFunctionView();
        });
        mViewModel.keyFunctionControlModel_L_T_LiveData.observe(this, keyFunctionControlModel_L_T_LiveData -> {
            keyFunctionControlModel_L_T = keyFunctionControlModel_L_T_LiveData;
            changeKeyFunctionView();
        });
        mViewModel.keyFunctionControlModel_R_T_LiveData.observe(this, keyFunctionControlModel_R_T_LiveData -> {
            keyFunctionControlModel_R_T = keyFunctionControlModel_R_T_LiveData;
            changeKeyFunctionView();
        });

        mViewModel.denoiseMutableLiveData.observe(this, denoiseModeDenoiseModeLevelPair -> {
            denoiseMode = denoiseModeDenoiseModeLevelPair.getFirst();
            denoiseModeLevel = denoiseModeDenoiseModeLevelPair.getSecond();
            changeDenoiseView();
        });
        mViewModel.denoiseSetResultMutableLiveData.observe(this, denoiseSetResultMutableLiveData -> {
            if (denoiseSetResultMutableLiveData == 0) {
                changeDenoiseView();
            }
        });
        mViewModel.findDeviceModelResultMutableLiveData.observe(this, findDeviceModelResultMutableLiveData -> {
            changeFindDeviceView();
        });

        mViewModel.isGameModelOpenLiveData.observe(this, isGameModelOpenLiveData -> {
            binding.switchGame.setChecked(isGameModelOpenLiveData);
        });
        mViewModel.isEarDetectionOpenLiveData.observe(this, isEarDetectionOpenLiveData -> {
            binding.switchEarCheck.setChecked(isEarDetectionOpenLiveData);
        });

        mViewModel.sleepModelMutableLiveData.observe(this, sleepModelMutableLiveData -> {
            sleepModel = sleepModelMutableLiveData;
            changeSleepView();
        });
        mViewModel.sleepModelSetResultMutableLiveData.observe(this, sleepModelSetResultMutableLiveData -> {
            if (sleepModelSetResultMutableLiveData == 0) {
                changeSleepView();
            }
        });
        mViewModel.spaceModelMutableLiveData.observe(this, spaceModelMutableLiveData -> {
            spaceModel = spaceModelMutableLiveData;
            changeSpaceSoundView();
        });
        mViewModel.immersionModelMutableLiveData.observe(this, immersionModelMutableLiveData -> {
            immersionModel = immersionModelMutableLiveData;
            changeImmersionSoundView();
        });
        mViewModel.volumeMutableLiveData.observe(this, volumeMutableLiveData -> {
            binding.includeVolumeSet.tvVolume.setText(volumeMutableLiveData + "%");
        });
        mViewModel.tfModelMutableLiveData.observe(this, tfModelMutableLiveData -> {
            mTFModel = tfModelMutableLiveData;
            changeTFView();
        });
        mViewModel.lightModelMutableLiveData.observe(this, lightModelMutableLiveData -> {
            mLightModel = lightModelMutableLiveData;
            changeLightView();
        });
        mViewModel.isFMOpenLiveData.observe(this, isFMOpenLiveData -> {
            binding.includeFmSet.switchFm.setChecked(isFMOpenLiveData);
        });

        mViewModel.logStringLiveData.observe(this, logStringLiveData -> {
            if (logStringLiveData.getSecond()) {
                autoScrollTextView.appendText("\n" + logStringLiveData.getFirst(), Color.RED, true);
            } else {
                autoScrollTextView.appendText("\n" + logStringLiveData.getFirst(), Color.BLACK, true);
            }
            autoScrollTextView.scrollToBottom();
        });
    }

    private void initData() {
        deviceConfigBean = (DeviceConfigBean) getIntent().getSerializableExtra(DemoScanBLEActivity.DEVICE_CONFIG);
        BluetoothHelper.getInstance().setWriteCallback(this);
        BluetoothHelper.getInstance().setGattCallback(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BluetoothHelper.getInstance().destroy();
    }


    /********************************************功能调试方法实现********************************************/

    // 添加 ProgressDialog 成员变量
    private ProgressDialog progressDialog;

    private void showProgressDialog() {

        if (progressDialog == null) {
            // 初始化 ProgressDialog
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在执行耗时操作，请等待...");
            progressDialog.setCancelable(false);
        }
        // 显示 ProgressDialog
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    private void dismissProgressDialog() {
        // 隐藏 ProgressDialog
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    private void oneTestGetState() {
        showProgressDialog();
        mViewModel.sendTestCMD(() -> {
            binding.getRoot().postDelayed(() -> {
                mViewModel.checkTestCMDResultLogOutput();
                dismissProgressDialog();
                ToastUtils.show("一键测试完毕(获取状态)！！！");
            }, 2000);
        }, deviceConfigBean, true);
    }

    private void oneTestFunction() {
        showProgressDialog();
        mViewModel.sendTestCMD(() -> {
            binding.getRoot().postDelayed(() -> {
                mViewModel.checkTestCMDResultLogOutput();
                dismissProgressDialog();
                ToastUtils.show("一键测试完毕（功能设置）！！！");
            }, 2000);

        }, deviceConfigBean, false);
    }


    private LightModel mLightModel = LightModel.DefModel;

    private void changeLightView() {
        switch (mLightModel) {
            case DefModel:
                binding.includeLightSet.lightRadioButton1.setChecked(true);
                break;
            case Light1:
                binding.includeLightSet.lightRadioButton2.setChecked(true);
                break;
            case Light2:
                binding.includeLightSet.lightRadioButton3.setChecked(true);
                break;
            case Light3:
                binding.includeLightSet.lightRadioButton4.setChecked(true);
                break;
            case Light4:
                binding.includeLightSet.lightRadioButton5.setChecked(true);
                break;
            case Light5:
                binding.includeLightSet.lightRadioButton6.setChecked(true);
                break;
        }
    }

    private void sendLightSetCMD(View view) {
        if (view.getId() == R.id.light_radio_button_1) {
            mLightModel = LightModel.DefModel;
        } else if (view.getId() == R.id.light_radio_button_2) {
            mLightModel = LightModel.Light1;
        } else if (view.getId() == R.id.light_radio_button_3) {
            mLightModel = LightModel.Light2;
        } else if (view.getId() == R.id.light_radio_button_4) {
            mLightModel = LightModel.Light3;
        } else if (view.getId() == R.id.light_radio_button_5) {
            mLightModel = LightModel.Light4;
        } else if (view.getId() == R.id.light_radio_button_6) {
            mLightModel = LightModel.Light5;
        }
        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getLightSetCMD(mLightModel));
    }


    private TFModel mTFModel = TFModel.Bluetooth;

    private void changeTFView() {
        switch (mTFModel) {
            case TF:
                binding.includeTfBluetooth.tfBluetoothRadioButton1.setChecked(true);
                break;
            case Bluetooth:
                binding.includeTfBluetooth.tfBluetoothRadioButton2.setChecked(true);
                break;
        }
    }

    private void sendTFModelCMD(View view) {
        if (view.getId() == R.id.tf_bluetooth_radio_button_1) {
            mTFModel = TFModel.TF;
        } else if (view.getId() == R.id.tf_bluetooth_radio_button_2) {
            mTFModel = TFModel.Bluetooth;
        }
        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getTFSetCMD(mTFModel));
    }


    private void sendGetBatteryCMD() {
        if (deviceConfigBean.getDeviceType() == 2) { //音箱
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_BOX_BATTERY, true);
        } else if (deviceConfigBean.getDeviceType() == 1) {  //骨气传导、头戴式耳机
            BluetoothHelper.getInstance().sendDataSafety(
                    BluetoothDataUtil.BLE_CMD_GET_HEADSET_BATTERY, true);
        }
    }


    private ImmersionModel immersionModel = ImmersionModel.Off;

    private void changeImmersionSoundView() {
        switch (immersionModel) {
            case Off:
                binding.includeImmersionSound.immersionRadioButton1.setChecked(true);
                break;
            case Statical:
                binding.includeImmersionSound.immersionRadioButton2.setChecked(true);
                break;
            case Dynamic:
                binding.includeImmersionSound.immersionRadioButton3.setChecked(true);
                break;
        }
    }

    private void sendImmersionSoundCMD(View view) {
        if (view.getId() == R.id.immersion_radio_button_1) {
            immersionModel = ImmersionModel.Off;
        } else if (view.getId() == R.id.immersion_radio_button_2) {
            immersionModel = ImmersionModel.Statical;
        } else if (view.getId() == R.id.immersion_radio_button_3) {
            immersionModel = ImmersionModel.Dynamic;
        }

        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getImmersionModelSetCMD(immersionModel));
    }


    private SpaceModel spaceModel = SpaceModel.Music;

    private void changeSpaceSoundView() {
        switch (spaceModel) {
            case Music:
                binding.includeSpaceSound.spaceSoundRadioButton1.setChecked(true);
                break;
            case Sport:
                binding.includeSpaceSound.spaceSoundRadioButton2.setChecked(true);
                break;
            case Video:
                binding.includeSpaceSound.spaceSoundRadioButton3.setChecked(true);
                break;
            case Empty:
                break;
        }
    }

    private void sendSpaceSoundCMD(View view) {
        if (view.getId() == R.id.space_sound_radio_button_1) {
            spaceModel = SpaceModel.Music;
        } else if (view.getId() == R.id.space_sound_radio_button_2) {
            spaceModel = SpaceModel.Sport;
        } else if (view.getId() == R.id.space_sound_radio_button_3) {
            spaceModel = SpaceModel.Video;
        }
        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getSpaceModelSetCMD(spaceModel));
    }


    private SleepModel sleepModel = SleepModel.Standard;

    private void changeSleepView() {
        switch (sleepModel) {
            case Standard:
                binding.includeSleepModel.sleepRadioButton1.setChecked(true);
                break;
            case Sleep:
                binding.includeSleepModel.sleepRadioButton2.setChecked(true);
                break;
            case LowPower:
                binding.includeSleepModel.sleepRadioButton3.setChecked(true);
                break;
        }
    }

    private void sendSleepCMD(View view) {
        if (view.getId() == R.id.sleep_radio_button_1) {
            sleepModel = SleepModel.Standard;
        } else if (view.getId() == R.id.sleep_radio_button_2) {
            sleepModel = SleepModel.Sleep;
        } else if (view.getId() == R.id.sleep_radio_button_3) {
            sleepModel = SleepModel.LowPower;
        }

        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getSleepSetCMD(sleepModel));
    }


    private void setSleepTime() {
        String hStr = binding.includeSleepModel.etInputH.getText().toString().trim();
        String mStr = binding.includeSleepModel.etInputMin.getText().toString().trim();
        if (hStr.isEmpty() || mStr.isEmpty()) {
            ToastUtils.show("请输入时间");
            return;
        }

        int hour = Integer.parseInt(hStr);
        int minute = Integer.parseInt(mStr);
        if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
            ToastUtils.show("请输入正确的时间");
            return;
        }
        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getSleepTimeCMD(hour, minute));
    }


    private FindDeviceModel findDeviceModel = FindDeviceModel.CANCEL;

    private void changeFindDeviceView() {
        binding.includeFindDevice.findCheckBox1.setChecked(false);
        binding.includeFindDevice.findCheckBox2.setChecked(false);

        switch (findDeviceModel) {
            case LEFT_EAR:
                binding.includeFindDevice.findCheckBox1.setChecked(true);
                binding.includeFindDevice.findCheckBox2.setChecked(false);
                break;
            case RIGHT_EAR:
                binding.includeFindDevice.findCheckBox1.setChecked(false);
                binding.includeFindDevice.findCheckBox2.setChecked(true);
                break;
            case LEFT_AND_RIGHT_EAR:
                binding.includeFindDevice.findCheckBox1.setChecked(true);
                binding.includeFindDevice.findCheckBox2.setChecked(true);
                break;
            case CANCEL:
                binding.includeFindDevice.findCheckBox1.setChecked(false);
                binding.includeFindDevice.findCheckBox2.setChecked(false);
                break;
        }
    }

    private void sendFindDeviceCMD(View view) {
        if (view.getId() == R.id.btn_cancel_find) {
            findDeviceModel = FindDeviceModel.CANCEL;
            binding.includeFindDevice.findCheckBox1.setChecked(false);
            binding.includeFindDevice.findCheckBox2.setChecked(false);
        } else {
            boolean leftChecked = binding.includeFindDevice.findCheckBox1.isChecked();
            boolean rightChecked = binding.includeFindDevice.findCheckBox2.isChecked();

            if (leftChecked && rightChecked) {
                findDeviceModel = FindDeviceModel.LEFT_AND_RIGHT_EAR;
            } else if (leftChecked) {
                findDeviceModel = FindDeviceModel.LEFT_EAR;
            } else if (rightChecked) {
                findDeviceModel = FindDeviceModel.RIGHT_EAR;
            } else {
                findDeviceModel = FindDeviceModel.CANCEL;
            }
        }

        byte[] mCMD = BluetoothDataUtil.getFindDeviceSetCMD(findDeviceModel);
        BluetoothHelper.getInstance().sendByteArrayDataSafety(mCMD);
    }


    private DenoiseMode denoiseMode = DenoiseMode.NOISE_MODE_DENOISE;
    private DenoiseModeLevel denoiseModeLevel = DenoiseModeLevel.NOISE_MODE_LEVEL_MILD;

    private void changeDenoiseView() {
        binding.includeDenoiseSet.levelRadioButton1.setChecked(false);
        binding.includeDenoiseSet.levelRadioButton2.setChecked(false);
        binding.includeDenoiseSet.levelRadioButton3.setChecked(false);

        switch (denoiseMode) {
            case NOISE_MODE_DENOISE:
                binding.includeDenoiseSet.denoiseRadioButton1.setChecked(true);
                break;
            case NOISE_MODE_TRANSPARENT:
                binding.includeDenoiseSet.denoiseRadioButton2.setChecked(true);
                break;
            case NOISE_MODE_OFF:
                binding.includeDenoiseSet.denoiseRadioButton3.setChecked(true);
                break;
        }

        switch (denoiseModeLevel) {
            case NOISE_MODE_LEVEL_MILD:
                binding.includeDenoiseSet.levelRadioButton1.setChecked(true);
                break;
            case NOISE_MODE_LEVEL_MEDIUM:
                binding.includeDenoiseSet.levelRadioButton2.setChecked(true);
                break;
            case NOISE_MODE_LEVEL_SEVERE:
                binding.includeDenoiseSet.levelRadioButton3.setChecked(true);
                break;
            case NOISE_MODE_LEVEL_NULL:
                break;
        }
    }

    private void sendDenoiseCMD(View radioButton) {
        if (radioButton.getId() == R.id.denoise_radio_button_1) {
            denoiseMode = DenoiseMode.NOISE_MODE_DENOISE;
        } else if (radioButton.getId() == R.id.denoise_radio_button_2) {
            denoiseMode = DenoiseMode.NOISE_MODE_TRANSPARENT;
        } else if (radioButton.getId() == R.id.denoise_radio_button_3) {
            denoiseMode = DenoiseMode.NOISE_MODE_OFF;
        } else if (radioButton.getId() == R.id.level_radio_button_1) {
            denoiseModeLevel = DenoiseModeLevel.NOISE_MODE_LEVEL_MILD;
        } else if (radioButton.getId() == R.id.level_radio_button_2) {
            denoiseModeLevel = DenoiseModeLevel.NOISE_MODE_LEVEL_MEDIUM;
        } else if (radioButton.getId() == R.id.level_radio_button_3) {
            denoiseModeLevel = DenoiseModeLevel.NOISE_MODE_LEVEL_SEVERE;
        }

        byte[] eqSetCMD = BluetoothDataUtil.getDenoiseSetCMD(denoiseMode, denoiseModeLevel);
        BluetoothHelper.getInstance().sendByteArrayDataSafety(eqSetCMD);
    }


    private void changeEQView() {
        binding.includeEqSet.eqRadioButton1.setChecked(false);
        binding.includeEqSet.eqRadioButton2.setChecked(false);
        binding.includeEqSet.eqRadioButton3.setChecked(false);
        binding.includeEqSet.eqRadioButton4.setChecked(false);
        binding.includeEqSet.eqRadioButton5.setChecked(false);
        binding.includeEqSet.eqRadioButton6.setChecked(false);
        switch (mEQModel) {
            case SSQVP:
                binding.includeEqSet.eqRadioButton1.setChecked(true);
                break;
            case POP:
                binding.includeEqSet.eqRadioButton2.setChecked(true);
                break;
            case ROCK:
                binding.includeEqSet.eqRadioButton3.setChecked(true);
                break;
            case CLASSIC:
                binding.includeEqSet.eqRadioButton4.setChecked(true);
                break;
            case LYRIC:
                binding.includeEqSet.eqRadioButton5.setChecked(true);
                break;
            case SOFT:
                binding.includeEqSet.eqRadioButton6.setChecked(true);
                break;
        }
    }


    private void sendEQCMD(View radioButton) {
        if (radioButton.getId() == R.id.eq_radio_button_1) {
            mEQModel = EQModel.SSQVP;
        } else if (radioButton.getId() == R.id.eq_radio_button_2) {
            mEQModel = EQModel.POP;
        } else if (radioButton.getId() == R.id.eq_radio_button_3) {
            mEQModel = EQModel.ROCK;
        } else if (radioButton.getId() == R.id.eq_radio_button_4) {
            mEQModel = EQModel.CLASSIC;
        } else if (radioButton.getId() == R.id.eq_radio_button_5) {
            mEQModel = EQModel.LYRIC;
        } else if (radioButton.getId() == R.id.eq_radio_button_6) {
            mEQModel = EQModel.SOFT;
        }

        byte[] eqSetCMD = BluetoothDataUtil.getEQSetCMD(mEQModel, deviceConfigBean.getDeviceType());
        BluetoothHelper.getInstance().sendByteArrayDataSafety(eqSetCMD);
    }

    private KeyFunctionControlModel keyFunctionControlModel_L_D = KeyFunctionControlModel.Previous;   // 左双击->上一曲
    private KeyFunctionControlModel keyFunctionControlModel_R_D = KeyFunctionControlModel.Next;       // 右双击->下一曲
    private KeyFunctionControlModel keyFunctionControlModel_L_T = KeyFunctionControlModel.VolumeUp;   // 左三击->音量加
    private KeyFunctionControlModel keyFunctionControlModel_R_T = KeyFunctionControlModel.VolumeDown; // 右三击->音量减

    private void changeKeyFunctionView() {
        switch (keyFunctionControlModel_L_D) {
            case PlayOrPause:
                binding.includeKeyFunctionSet.leftDoubleRadioOption1.setChecked(true);
                break;
            case Previous:
                binding.includeKeyFunctionSet.leftDoubleRadioOption2.setChecked(true);
                break;
            case Next:
                binding.includeKeyFunctionSet.leftDoubleRadioOption3.setChecked(true);
                break;
            case VolumeUp:
                binding.includeKeyFunctionSet.leftDoubleRadioOption4.setChecked(true);
                break;
            case VolumeDown:
                binding.includeKeyFunctionSet.leftDoubleRadioOption5.setChecked(true);
                break;
        }

        switch (keyFunctionControlModel_R_D) {
            case PlayOrPause:
                binding.includeKeyFunctionSet.rightDoubleRadioOption1.setChecked(true);
                break;
            case Previous:
                binding.includeKeyFunctionSet.rightDoubleRadioOption2.setChecked(true);
                break;
            case Next:
                binding.includeKeyFunctionSet.rightDoubleRadioOption3.setChecked(true);
                break;
            case VolumeUp:
                binding.includeKeyFunctionSet.rightDoubleRadioOption4.setChecked(true);
                break;
            case VolumeDown:
                binding.includeKeyFunctionSet.rightDoubleRadioOption5.setChecked(true);
                break;
        }

        switch (keyFunctionControlModel_L_T) {
            case PlayOrPause:
                binding.includeKeyFunctionSet.leftTripleRadioOption1.setChecked(true);
                break;
            case Previous:
                binding.includeKeyFunctionSet.leftTripleRadioOption2.setChecked(true);
                break;
            case Next:
                binding.includeKeyFunctionSet.leftTripleRadioOption3.setChecked(true);
                break;
            case VolumeUp:
                binding.includeKeyFunctionSet.leftTripleRadioOption4.setChecked(true);
                break;
            case VolumeDown:
                binding.includeKeyFunctionSet.leftTripleRadioOption5.setChecked(true);
                break;
        }

        switch (keyFunctionControlModel_R_T) {
            case PlayOrPause:
                binding.includeKeyFunctionSet.rightTripleRadioOption1.setChecked(true);
                break;
            case Previous:
                binding.includeKeyFunctionSet.rightTripleRadioOption2.setChecked(true);
                break;
            case Next:
                binding.includeKeyFunctionSet.rightTripleRadioOption3.setChecked(true);
                break;
            case VolumeUp:
                binding.includeKeyFunctionSet.rightTripleRadioOption4.setChecked(true);
                break;
            case VolumeDown:
                binding.includeKeyFunctionSet.rightTripleRadioOption5.setChecked(true);
                break;
        }
    }


    private void sendKeyFunctionCMD(View radioButton) {
        KeyFunctionClickModel keyFunctionClickModel = KeyFunctionClickModel.LeftDoubleClick;
        KeyFunctionControlModel keyFunctionControlModel = KeyFunctionControlModel.PlayOrPause;
        if (radioButton.getId() == R.id.left_double_radio_option1) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.PlayOrPause;
            keyFunctionControlModel_L_D = KeyFunctionControlModel.PlayOrPause;
        } else if (radioButton.getId() == R.id.left_double_radio_option2) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.Previous;
            keyFunctionControlModel_L_D = KeyFunctionControlModel.Previous;
        } else if (radioButton.getId() == R.id.left_double_radio_option3) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.Next;
            keyFunctionControlModel_L_D = KeyFunctionControlModel.Next;
        } else if (radioButton.getId() == R.id.left_double_radio_option4) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeUp;
            keyFunctionControlModel_L_D = KeyFunctionControlModel.VolumeUp;
        } else if (radioButton.getId() == R.id.left_double_radio_option5) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeDown;
            keyFunctionControlModel_L_D = KeyFunctionControlModel.VolumeDown;

        } else if (radioButton.getId() == R.id.left_triple_radio_option1) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.PlayOrPause;
            keyFunctionControlModel_L_T = KeyFunctionControlModel.PlayOrPause;
        } else if (radioButton.getId() == R.id.left_triple_radio_option2) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.Previous;
            keyFunctionControlModel_L_T = KeyFunctionControlModel.Previous;
        } else if (radioButton.getId() == R.id.left_triple_radio_option3) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.Next;
            keyFunctionControlModel_L_T = KeyFunctionControlModel.Next;
        } else if (radioButton.getId() == R.id.left_triple_radio_option4) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeUp;
            keyFunctionControlModel_L_T = KeyFunctionControlModel.VolumeUp;
        } else if (radioButton.getId() == R.id.left_triple_radio_option5) {
            keyFunctionClickModel = KeyFunctionClickModel.LeftThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeDown;
            keyFunctionControlModel_L_T = KeyFunctionControlModel.VolumeDown;

        } else if (radioButton.getId() == R.id.right_double_radio_option1) {
            keyFunctionClickModel = KeyFunctionClickModel.RightDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.PlayOrPause;
            keyFunctionControlModel_R_D = KeyFunctionControlModel.PlayOrPause;
        } else if (radioButton.getId() == R.id.right_double_radio_option2) {
            keyFunctionClickModel = KeyFunctionClickModel.RightDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.Previous;
            keyFunctionControlModel_R_D = KeyFunctionControlModel.Previous;
        } else if (radioButton.getId() == R.id.right_double_radio_option3) {
            keyFunctionClickModel = KeyFunctionClickModel.RightDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.Next;
            keyFunctionControlModel_R_D = KeyFunctionControlModel.Next;
        } else if (radioButton.getId() == R.id.right_double_radio_option4) {
            keyFunctionClickModel = KeyFunctionClickModel.RightDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeUp;
            keyFunctionControlModel_R_D = KeyFunctionControlModel.VolumeUp;
        } else if (radioButton.getId() == R.id.right_double_radio_option5) {
            keyFunctionClickModel = KeyFunctionClickModel.RightDoubleClick;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeDown;
            keyFunctionControlModel_R_D = KeyFunctionControlModel.VolumeDown;

        } else if (radioButton.getId() == R.id.right_triple_radio_option1) {
            keyFunctionClickModel = KeyFunctionClickModel.RightThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.PlayOrPause;
            keyFunctionControlModel_R_T = KeyFunctionControlModel.PlayOrPause;
        } else if (radioButton.getId() == R.id.right_triple_radio_option2) {
            keyFunctionClickModel = KeyFunctionClickModel.RightThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.Previous;
            keyFunctionControlModel_R_T = KeyFunctionControlModel.Previous;
        } else if (radioButton.getId() == R.id.right_triple_radio_option3) {
            keyFunctionClickModel = KeyFunctionClickModel.RightThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.Next;
            keyFunctionControlModel_R_T = KeyFunctionControlModel.Next;
        } else if (radioButton.getId() == R.id.right_triple_radio_option4) {
            keyFunctionClickModel = KeyFunctionClickModel.RightThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeUp;
            keyFunctionControlModel_R_T = KeyFunctionControlModel.VolumeUp;
        } else if (radioButton.getId() == R.id.right_triple_radio_option5) {
            keyFunctionClickModel = KeyFunctionClickModel.RightThreeShot;
            keyFunctionControlModel = KeyFunctionControlModel.VolumeDown;
            keyFunctionControlModel_R_T = KeyFunctionControlModel.VolumeDown;
        }

        byte[] mCMD = BluetoothDataUtil.getKeyFunctionSetCMD(keyFunctionClickModel, keyFunctionControlModel);
        BluetoothHelper.getInstance().sendByteArrayDataSafety(mCMD);
    }


    private void initView() {
        autoScrollTextView = findViewById(R.id.tv_log);
        binding.ivFullUi.setOnClickListener(onClickListener);
        binding.ivBack.setOnClickListener(onClickListener);
        binding.btnSave.setOnClickListener(onClickListener);
        binding.ivClean.setOnClickListener(onClickListener);
        binding.ivSendText.setOnClickListener(onClickListener);

        binding.tvDeviceName.setText(BluetoothHelper.getmConnectedBleDevice().getName());
        binding.tvDeviceBleMac.setText("Mac:" + BluetoothHelper.getmConnectedBleDevice().getMac());

        binding.includeEqSet.eqRadioButton1.setOnClickListener(onClickListener);
        binding.includeEqSet.eqRadioButton2.setOnClickListener(onClickListener);
        binding.includeEqSet.eqRadioButton3.setOnClickListener(onClickListener);
        binding.includeEqSet.eqRadioButton4.setOnClickListener(onClickListener);
        binding.includeEqSet.eqRadioButton5.setOnClickListener(onClickListener);
        binding.includeEqSet.eqRadioButton6.setOnClickListener(onClickListener);
        binding.includeEqSet.btnGetEq.setOnClickListener(onClickListener);

        binding.includeKeyFunctionSet.leftDoubleRadioOption1.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftDoubleRadioOption2.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftDoubleRadioOption3.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftDoubleRadioOption4.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftDoubleRadioOption5.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightDoubleRadioOption1.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightDoubleRadioOption2.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightDoubleRadioOption3.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightDoubleRadioOption4.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightDoubleRadioOption5.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftTripleRadioOption1.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftTripleRadioOption2.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftTripleRadioOption3.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftTripleRadioOption4.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.leftTripleRadioOption5.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightTripleRadioOption1.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightTripleRadioOption2.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightTripleRadioOption3.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightTripleRadioOption4.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.rightTripleRadioOption5.setOnClickListener(onClickListener);
        binding.includeKeyFunctionSet.btnKeyFunctionCurrentState.setOnClickListener(onClickListener);

        binding.includeFindDevice.btnCancelFind.setOnClickListener(onClickListener);
        binding.includeFmSet.tvLastRadio.setOnClickListener(onClickListener);
        binding.includeFmSet.tvMuteSet.setOnClickListener(onClickListener);
        binding.includeFmSet.tvNextRadio.setOnClickListener(onClickListener);

        binding.btnDisconnectBle.setOnClickListener(onClickListener);
        binding.btnResetDevice.setOnClickListener(onClickListener);
        binding.switchEarCheck.setOnClickListener(onClickListener);
        binding.switchGame.setOnClickListener(onClickListener);

        binding.btnDoubleBattery.setOnClickListener(onClickListener);
        binding.btnBinBattery.setOnClickListener(onClickListener);

        binding.includeDenoiseSet.btnGetDenoise.setOnClickListener(onClickListener);
        binding.includeDenoiseSet.denoiseRadioButton1.setOnClickListener(onClickListener);
        binding.includeDenoiseSet.denoiseRadioButton2.setOnClickListener(onClickListener);
        binding.includeDenoiseSet.denoiseRadioButton3.setOnClickListener(onClickListener);
        binding.includeDenoiseSet.levelRadioButton1.setOnClickListener(onClickListener);
        binding.includeDenoiseSet.levelRadioButton2.setOnClickListener(onClickListener);
        binding.includeDenoiseSet.levelRadioButton3.setOnClickListener(onClickListener);

        binding.includeFindDevice.findCheckBox1.setOnClickListener(onClickListener);
        binding.includeFindDevice.findCheckBox2.setOnClickListener(onClickListener);
        binding.includeFindDevice.btnCancelFind.setOnClickListener(onClickListener);

        //睡眠模式
        binding.includeSleepModel.btnGetSleep.setOnClickListener(onClickListener);
        binding.includeSleepModel.sleepRadioButton1.setOnClickListener(onClickListener);
        binding.includeSleepModel.sleepRadioButton2.setOnClickListener(onClickListener);
        binding.includeSleepModel.sleepRadioButton3.setOnClickListener(onClickListener);
        binding.includeSleepModel.btnCancelTime.setOnClickListener(onClickListener);
        binding.includeSleepModel.btnSendSleepTime.setOnClickListener(onClickListener);

        //空间音效
        binding.includeSpaceSound.btnGetSpaceSound.setOnClickListener(onClickListener);
        binding.includeSpaceSound.spaceSoundRadioButton1.setOnClickListener(onClickListener);
        binding.includeSpaceSound.spaceSoundRadioButton2.setOnClickListener(onClickListener);
        binding.includeSpaceSound.spaceSoundRadioButton3.setOnClickListener(onClickListener);
        //沉浸式音频
        binding.includeImmersionSound.btnGetImmersion.setOnClickListener(onClickListener);
        binding.includeImmersionSound.immersionRadioButton1.setOnClickListener(onClickListener);
        binding.includeImmersionSound.immersionRadioButton2.setOnClickListener(onClickListener);
        binding.includeImmersionSound.immersionRadioButton3.setOnClickListener(onClickListener);

        binding.switchFocusModel.setOnClickListener(onClickListener);
        binding.switchAiTranslate.setOnClickListener(onClickListener);  //AI翻译
        binding.switchAiAudioTranscription.setOnClickListener(onClickListener); //录音转写

        binding.includeVolumeSet.btnGetVolume.setOnClickListener(onClickListener);
        binding.includeVolumeSet.btnVolumeAdd.setOnClickListener(onClickListener);
        binding.includeVolumeSet.btnVolumeSub.setOnClickListener(onClickListener);
        binding.includeVolumeSet.sbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                binding.includeVolumeSet.tvVolume.setText(progress + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                if (progress >= 0 && progress <= 100) {
                    BluetoothHelper.getInstance().sendByteArrayDataSafety(BluetoothDataUtil.getVolumeSetCMD(progress));
                }
            }
        });

        //播放控制
        binding.includePlayControl.btnPlay.setOnClickListener(onClickListener);
        binding.includePlayControl.btnPause.setOnClickListener(onClickListener);
        binding.includePlayControl.btnGetPlayState.setOnClickListener(onClickListener);
        binding.includePlayControl.btnLast.setOnClickListener(onClickListener);
        binding.includePlayControl.btnPlayPause.setOnClickListener(onClickListener);
        binding.includePlayControl.btnNext.setOnClickListener(onClickListener);

        binding.btnSingleBattery.setOnClickListener(onClickListener);

        binding.includeTfBluetooth.btnGetTfModel.setOnClickListener(onClickListener);
        binding.includeTfBluetooth.tfBluetoothRadioButton1.setOnClickListener(onClickListener);
        binding.includeTfBluetooth.tfBluetoothRadioButton2.setOnClickListener(onClickListener);

        //灯光设置
        binding.includeLightSet.btnGetLight.setOnClickListener(onClickListener);
        binding.includeLightSet.lightRadioButton1.setOnClickListener(onClickListener);
        binding.includeLightSet.lightRadioButton2.setOnClickListener(onClickListener);
        binding.includeLightSet.lightRadioButton3.setOnClickListener(onClickListener);
        binding.includeLightSet.lightRadioButton4.setOnClickListener(onClickListener);
        binding.includeLightSet.lightRadioButton5.setOnClickListener(onClickListener);
        binding.includeLightSet.lightRadioButton6.setOnClickListener(onClickListener);

        //FM设置
        binding.includeFmSet.btnGetFm.setOnClickListener(onClickListener);
        binding.includeFmSet.btnGetRadio.setOnClickListener(onClickListener);
        binding.includeFmSet.tvLastRadio.setOnClickListener(onClickListener);
        binding.includeFmSet.tvMuteSet.setOnClickListener(onClickListener);
        binding.includeFmSet.tvNextRadio.setOnClickListener(onClickListener);
        binding.includeFmSet.switchFm.setOnClickListener(onClickListener);

        //一键操作
        binding.btnOneTestGetState.setOnClickListener(onClickListener);
        binding.btnOneTestFuction.setOnClickListener(onClickListener);
    }

    private void updateUIByConfigData() {
        if (deviceConfigBean == null) {
            LogUtil.e(TAG + " deviceName is null");
            return;
        }

        List<Integer> supportList = deviceConfigBean.getSupportList();
        supportList.forEach(supportId -> {
            switch (supportId) {
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0105:
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_0206:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_0306:
                    binding.llBleDisconnect.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0106:
                    binding.llEarCheck.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0107:
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0108:
                    binding.llGameModel.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0109:
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_0207:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_0309:
                    binding.llResetDevice.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_010a:
                    binding.llGetDoubleBattery.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_010b:
                    binding.llGetBinBattery.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_010c:
                    binding.includeKeyFunctionSet.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_010d:
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_0203:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_030e:
                    binding.includeEqSet.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_010e:
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_020b:
                    binding.includeDenoiseSet.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_010f:
                    binding.includeFindDevice.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0110:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_030b:
                    binding.includeSleepModel.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0111:
                    binding.includeSpaceSound.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0112:
                    binding.includeImmersionSound.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0113:
                    binding.llFocusModel.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0114:
                    binding.llAiTranslate.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.TWS_SUPPORT_ID_0115:
                    binding.llAiAudioTranscription.setVisibility(View.VISIBLE);
                    break;

                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_0201:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_0301:
                    binding.includeVolumeSet.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_0202:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_0302:
                    binding.includePlayControl.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_0208:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_0307:
                    binding.llGetSingleBattery.setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.HEADSET_SUPPORT_ID_020a:
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_030a:
                    binding.includeTfBluetooth.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_030c:
                    binding.includeLightSet.getRoot().setVisibility(View.VISIBLE);
                    break;
                case NovaDeviceConfigMessage.BOX_SUPPORT_ID_030d:
                    binding.includeFmSet.getRoot().setVisibility(View.VISIBLE);
                    break;
            }
        });


    }

    private boolean isFull = false;

    private void setFull() {
        if (isFull) {
            autoScrollTextView.setMaxHeight(dpToPx(DemoBluetoothControlActivity.this, 150));
            findViewById(R.id.ll_function).setVisibility(View.VISIBLE);
        } else {
            // 假设 autoScrollTextView 和 其父容器已经初始化
            View parentView = findViewById(R.id.main);
            int parentHeight = parentView.getHeight();
            autoScrollTextView.setMaxHeight(parentHeight - 250);
            findViewById(R.id.ll_function).setVisibility(View.GONE);
        }
        isFull = !isFull;
    }

    private int i = 0;

    private void test() {
        new Thread(() -> {
            while (true) {
                try {
                    i++;
                    autoScrollTextView.post(() -> {
                        if (i % 2 == 0) {
                            autoScrollTextView.appendText("\n测试数据" + i);
                        } else {
                            autoScrollTextView.appendText("\n测试数据" + i, Color.RED, true);
                        }
                        autoScrollTextView.scrollToBottom();
                    });

                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (i > 500) {
                    break;
                }
            }
        }).start();

    }

    private void doBtnSave() {
        getReadMediaPermission(new com.nova.bluetooth.permission.OnPermissionListener() {
            @Override
            public void onPermissionGranted(PermissionType permissionType) {
                showOutputLogDialog();
            }

            @Override
            public void onPermissionDenied(PermissionType permissionType) {
                ToastUtils.show("未获得文件读写权限，无法操作下一步");
            }
        }, "");
    }

    private void doSendText() {
        String hexStr = binding.etInputName.getText().toString();
        if (hexStr.isEmpty()) {
            ToastUtils.show("请输入要发送的内容");
            return;
        }

        if (hexStr.length() % 2 != 0) {
            ToastUtils.show("数据未通过验证，不发送");
            return;
        }

        BluetoothHelper.getInstance().sendDataSafety(binding.etInputName.getText().toString(), false);
        ToastUtils.show("发送已触发");
    }

    private void showOutputLogDialog() {
        CommonDialog dialog = new CommonDialog(this, R.layout.dialog_output_log);
        dialog.setWidth(R.dimen.dp_320);
        dialog.setCancelable(true);
        dialog.setGravity(Gravity.CENTER);
        dialog.showDialog();
        dialog.getDialog().findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismissDialog();
            }
        });
        dialog.getDialog().findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveContentToFile(autoScrollTextView.getText().toString());
                dialog.dismissDialog();
            }
        });
    }


    public void saveContentToFile(String content) {
        new Thread(() -> {
            String fileName = "My_bluetooth_log_" + System.currentTimeMillis() + ".txt";
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), fileName);

            try (FileOutputStream fos = new FileOutputStream(file)) {
                fos.write(content.getBytes());
                Log.d("PermissionActivity", "saveContentToFile: " + file.getAbsolutePath());
                runOnUiThread(() -> ToastUtils.show("日志已保存到: " + file.getAbsolutePath()));
            } catch (IOException e) {
                runOnUiThread(() -> ToastUtils.show("保存日志失败: " + e.getMessage()));
            }
        }).start();
    }


    /*******************************************蓝牙数据交互**********************************************/
    @Override
    public void onWriteSuccess(int current, int total, byte[] justWrite) {
        autoScrollTextView.appendText("\n发送成功====>" + HexUtil.encodeHexStr(justWrite) + "\n");
        autoScrollTextView.scrollToBottom();
    }

    @Override
    public void onWriteFailure(BleException exception) {
        autoScrollTextView.appendText("\n发送失败:" + exception.toString());
        autoScrollTextView.scrollToBottom();
    }

    @Override
    public void onCharacteristicChanged(byte[] data) {
        autoScrollTextView.appendText("\n接收数据====>" + HexUtil.encodeHexStr(data) + "\n");
        autoScrollTextView.scrollToBottom();
    }

    @Override
    public void onStartConnect() {

    }

    @Override
    public void onConnectFail(BleDevice bleDevice, BleException exception) {

    }

    @Override
    public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {

    }

    @Override
    public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
        autoScrollTextView.appendText("\n断开连接:" + bleDevice.getMac(), Color.RED, true);
        autoScrollTextView.scrollToBottom();
    }

    @Override
    public void onConnectedNotifySuccess(BleDevice bleDevice) {
        //通知已打开，可以开始监听设备的数据变化了
    }
}